package com.stylefeng.guns.rest.modular.departdata.controller;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoRequest;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.stylefeng.guns.rest.common.CommonResponse;
import com.stylefeng.guns.rest.common.base.Constans;
import com.stylefeng.guns.rest.common.exception.BizExceptionEnum;
import com.stylefeng.guns.rest.common.exception.BussinessException;
import com.stylefeng.guns.rest.common.persistence.model.Article;
import com.stylefeng.guns.rest.common.persistence.model.DepartData;
import com.stylefeng.guns.rest.common.persistence.model.FileEntity;
import com.stylefeng.guns.rest.common.persistence.model.WithDrow;
import com.stylefeng.guns.rest.common.utils.PropertiesUtil;
import com.stylefeng.guns.rest.common.utils.TextUtil;
import com.stylefeng.guns.rest.common.utils.aliyunoss.OSSUploadUtil;
import com.stylefeng.guns.rest.modular.artitle.controller.dto.AccessRequest;
import com.stylefeng.guns.rest.modular.artitle.controller.dto.ArticleDetailResponse;
import com.stylefeng.guns.rest.modular.artitle.controller.dto.ArtitleBodyRequest;
import com.stylefeng.guns.rest.modular.artitle.service.ArticleService;
import com.stylefeng.guns.rest.modular.artitle.service.FileService;
import com.stylefeng.guns.rest.modular.attent.controller.dto.AttentHotRequest;
import com.stylefeng.guns.rest.modular.catalogy.controller.dto.ArticleRequest;
import com.stylefeng.guns.rest.modular.departdata.controller.dto.*;
import com.stylefeng.guns.rest.modular.departdata.service.DepartShowService;
import org.apache.commons.logging.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by ASUS-PC on 2019/9/14.
 */
@RestController
@RequestMapping("/departdata")
public class ArtitleDepartController {
    private final Log logger = org.apache.commons.logging.LogFactory.getLog(this.getClass());
    @Autowired
    DepartShowService departShowService;

    /**
     * 提交展示广告
     * @param request
     * @return
     */
    @RequestMapping(value = "/departshow", method = RequestMethod.POST)
    public ResponseEntity<?> departShowData(@RequestBody DepartShowRequest request){
        if(request.getUserId()==0 || request.getArticleId() == 0){
            throw new BussinessException(BizExceptionEnum.NEED_PARAMS);
        }
        //插入用户广告展示表
        if(departShowService.showDataInsert(request) >0){
            //更新总数
            departShowService.updateDepartData2(0,request);
            return ResponseEntity.ok(CommonResponse.ok("操作成功" , null));
        }else {
            throw new BussinessException(BizExceptionEnum.DELET_FAUIL);
        }
    }

    /**
     * 提交点击广告
     * @param request
     * @return
     */
    @RequestMapping(value = "/departclick", method = RequestMethod.POST)
    public ResponseEntity<?> getUserArticleList(@RequestBody DepartShowRequest request){
        if(request.getUserId()==0 || request.getArticleId() == 0){
            throw new BussinessException(BizExceptionEnum.NEED_PARAMS);
        }
        //插入用户广告点击表
        if(departShowService.clickDataInsert(request) >0){
            //更新总数
            departShowService.updateDepartData2(1,request);
            return ResponseEntity.ok(CommonResponse.ok("操作成功" , null));
        }else {
            throw new BussinessException(BizExceptionEnum.UNKNOWN_ERROR);
        }
    }

    /**
     * 获取用户近三个月的收益详情
     * @param request
     * @return
     */
    @RequestMapping(value = "/getUserDeparts", method = RequestMethod.POST)
    public ResponseEntity<?> getUserDepartList(@RequestBody DepartRequest request){
        if(request.getUserId()==0){
            throw new BussinessException(BizExceptionEnum.NEED_PARAMS);
        }
        if(request.getPage()==0){
            request.setPage(1);
        }
        if(request.getSize()==0){
            request.setSize(20);
        }
        List<DepartDataResponse> mList = departShowService.getUserDepartDatas(request);
        if(mList!=null && mList.size()>0){
            String localPara = PropertiesUtil.get("show_nums");
            DecimalFormat df = new DecimalFormat("0.00");
            for (DepartDataResponse d:mList) {
                double a = d.getShowNums() /Double.parseDouble(localPara);
                d.setMoney(df.format(a)+"");
            }
        }
        return ResponseEntity.ok(CommonResponse.ok(mList));
    }


    /**
     * 获取用户所有收益待提现金额
     * @param request
     * @return
     */
    @RequestMapping(value = "/getUserDepartMoney", method = RequestMethod.POST)
    public ResponseEntity<?> getUserDepartMoney(@RequestBody DepartRequest request){
        if(request.getUserId()==0){
            throw new BussinessException(BizExceptionEnum.NEED_PARAMS);
        }

        List<DepartData> mList = departShowService.getUserDepartMoney(request);
        HashMap<String,String> datas = new HashMap<>();
        if(mList!=null && mList.size()>0){
            String localPara = PropertiesUtil.get("show_nums");
            int nums = 0;
            for (DepartData d:mList) {
                nums =nums + d.getShowNums();
            }
            double a = nums/Double.parseDouble(localPara);
            logger.info("----->>"+nums+"----"+a);
            BigDecimal bg = new BigDecimal(a);
            double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            datas.put("account",f1+"");
            return ResponseEntity.ok(CommonResponse.ok(datas));
        }
        datas.put("account","0.00");
        return ResponseEntity.ok(CommonResponse.ok(datas));
    }


    /**
     * 用户提交提现金额
     * @param request
     * @return
     */
    @RequestMapping(value = "/userWithdrow", method = RequestMethod.POST)
    public ResponseEntity<?> commitWithdrow(@RequestBody WithdrowRequest request){
        if(request.getUserId()==0){
            throw new BussinessException(BizExceptionEnum.NEED_PARAMS);
        }
        //查询是否有审核中的提现记录
        boolean status = departShowService.getWithdrowingList(request.getUserId());
        if(status){
            throw new BussinessException(BizExceptionEnum.WITHDROWING_COMMIT);
        }
        WithDrow withDrow = new WithDrow();
        withDrow.setState(1);
        withDrow.setAlipay(request.getAlipay());
        withDrow.setPaytype(0);//默认支付宝
        withDrow.setCreateTime(new Date());
        withDrow.setWithdraw(request.getMoney());
        withDrow.setUserId(request.getUserId());
        DepartRequest departRequest = new DepartRequest();
        departRequest.setUserId(request.getUserId());
        List<DepartData> mList = departShowService.getUserDepartMoney(departRequest);
        if(mList!=null && mList.size()>0){
            String ids = "";
            for (DepartData d:mList) {
                ids = ids+d.getId()+",";
            }
            withDrow.setAccountIds(ids.substring(0,ids.length()-1));
        }
        int state = departShowService.insertWthdrow(withDrow);
        if(state>0){
            return ResponseEntity.ok(CommonResponse.ok(withDrow));
        }
        throw new BussinessException(BizExceptionEnum.UNKNOWN_ERROR);
    }

    /**
     * 用户提交提现金额明细
     * @param request
     * @return
     */
    @RequestMapping(value = "/userWithdrowList", method = RequestMethod.POST)
    public ResponseEntity<?> userWithdrowList(@RequestBody DepartRequest request){
        if(request.getUserId()==0){
            throw new BussinessException(BizExceptionEnum.NEED_PARAMS);
        }
        if(request.getPage()==0){
            request.setPage(1);
        }
        if(request.getSize()==0){
            request.setSize(20);
        }
        return ResponseEntity.ok(CommonResponse.ok(departShowService.userWithdrowList(request)));
    }


    /**
     * 用户提交提现金额明细
     * @param request
     * @return
     */
    @RequestMapping(value = "/userWithdrowDetail", method = RequestMethod.POST)
    public ResponseEntity<?> userWithdrowDetail(@RequestBody WithdrowDetailRequest request){
        if(request.getId()==0){
            throw new BussinessException(BizExceptionEnum.NEED_PARAMS);
        }
        return ResponseEntity.ok(CommonResponse.ok(departShowService.getWithDrowDetail(request)));
    }
}
